package org.softee.functional.samples;

import static org.softee.functional.FunctionalIterables.from;
import static org.softee.functional.functions.Predicates.matches;

import java.util.ArrayList;
import java.util.Collection;

import org.softee.functional.functions.BinaryOperators;
import org.softee.functional.functions.Mapper;
import org.softee.functional.functions.Predicate;

public class Wiki {

    public void sampleMapReduceCustom() {
        Collection<String> strings = new ArrayList<String>();
        // ...

        Mapper <String, Integer> stringLength = new Mapper<String, Integer>() {
            @Override
            public Integer apply(String input) {
                return input.length();
            }
        };

        Predicate<String> isUppercase = new Predicate<String>() {
            @Override
            public boolean apply(String input) {
                return (input != null) && input.toUpperCase().equals(input);
            }
        };

        // run filter/map/reduce operation in serial, using the current thread
        int longestUpperCase = from(strings)
                .filter(isUppercase)
                .map(stringLength)
                .reduce(0, BinaryOperators.<Integer>max());

        int longestUpperCase1 = from(strings)
                .filter(matches("[A-Z]+"))
                .mapReduce(stringLength, 0, BinaryOperators.<Integer>max());


        // run filter/map/reduce operation in parallel exploiting all processor cores
        int parallellongestUpperCase = from(strings)
                .parallel()
                .filter(isUppercase)
                .mapReduce(stringLength, 0, BinaryOperators.<Integer>max());
    }
}
